/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package doc2db.interfaces.action;

import doc2db.Keey;
import doc2db.UserProp;
import doc2db.fileWorker.FileBeginer;
import doc2db.interfaces.mainWindow.MyFrame;
import doc2db.interfaces.mainWindow.action.MyMouseListener;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.Box;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;

/**
 *
 * @author yuriy
 */
public class FontChooserAction extends JDialog implements ActionListener{
    private JLabel fonts = new JLabel("Шрифты");
    private JLabel fontsMebu = new JLabel("Шрифты меню");
    private JLabel fontsPane = new JLabel("Шрифты главной панели");
    private JLabel fontsText = new JLabel("Шрифты текстовых полей");
    private JButton fm = new JButton();
    private JButton fp = new JButton();
    private JButton ft = new JButton();
    private JButton cancel = new JButton("Закрыть", new ImageIcon(
            FileBeginer.resurse_resurseFolderPane + Keey.sp + "button_cancel.png"));
    private MyFrame frame;

    private StringBuffer start = new StringBuffer().append("<html>")
            .append("<table width=300>").append("<tr>")
            .append("<td align=\"left\"><font family=\"");
    private String end1 = "</td><td align=\"right\"><font style=\"monospased\">|";
    
    public FontChooserAction(JFrame f){
        super(f, "Шрифты", false);
        frame = (MyFrame) f;
        this.frameProp();
        this.initComponent();
        this.initVarAction();
        this.initVarFont();
        this.initVarSize();
    }

    private void frameProp(){
        setDefaultCloseOperation(JDialog.HIDE_ON_CLOSE);
        setResizable(true);
        setSize(340, 340);
        Toolkit kit = Toolkit.getDefaultToolkit();
        Dimension screnSize = kit.getScreenSize();
        int screanHeight = screnSize.height ;
        int screanWidth = screnSize.width ;
        setLocation((screanWidth / 2 - 170), (screanHeight / 2 - 170));
    }

    private void initComponent(){
        Box V = Box.createVerticalBox();
        V.add(fonts);
        V.add(Box.createHorizontalStrut(10));
        V.add(fontsMebu);
        V.add(Box.createVerticalStrut(3));
        V.add(fm);
        V.add(Box.createVerticalStrut(10));
        V.add(fontsPane);
        V.add(Box.createVerticalStrut(3));
        V.add(fp);
        V.add(Box.createVerticalStrut(10));
        V.add(fontsText);
        V.add(Box.createVerticalStrut(3));
        V.add(ft);
        V.add(Box.createGlue());
        getContentPane().add(V, BorderLayout.CENTER);
        Box h = Box.createHorizontalBox();
        h.add(Box.createGlue());
        h.add(cancel);
        h.add(Box.createHorizontalStrut(3));
        getContentPane().add(h, BorderLayout.SOUTH);
    }

    private void initVarAction(){
        cancel.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                setVisible(false);
            }
        });

        final JDialog d = this;
        fm.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                FontChooser2 fc = new FontChooser2(d, UserProp.menuFont);
                fc.setVisible(true);
                fc.dispose();
                if(! fc.isCancel){
                    UserProp.menuFont = fc.getSelectedFont();
                    frame.menu.setFont(UserProp.menuFont);
                    fm.setText(start.toString()+UserProp.menuFont.getName()+"\">"+UserProp
                        .menuFont.getName()+end1+UserProp.menuFont.getSize()+"</td>");
                }
            }
        });

        fp.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                FontChooser2 fc = new FontChooser2(d, UserProp.paneFont);
                fc.setVisible(true);
                fc.dispose();
                if(! fc.isCancel){
                    UserProp.paneFont = fc.getSelectedFont();
                    frame.pane.setFont(UserProp.paneFont);
                    fp.setText(start.toString()+UserProp.paneFont.getName()+"\">"+UserProp
                        .paneFont.getName()+end1+UserProp.paneFont.getSize()+"</td>");
                }
            }
        });

        ft.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                FontChooser2 fc = new FontChooser2(d, UserProp.textFont);
                fc.setVisible(true);
                fc.dispose();
                if(! fc.isCancel){
                    UserProp.textFont = fc.getSelectedFont();
                    ft.setText(start.toString()+UserProp.textFont.getName()+"\">"+UserProp
                        .textFont.getName()+end1+UserProp.textFont.getSize()+"</td>");
                }
            }
        });

        
    }

    private void initVarSize(){
        fm.setMinimumSize(new Dimension(310, 30));
        fm.setPreferredSize(fm.getMinimumSize());
        fm.setMaximumSize(fm.getPreferredSize());
        fp.setMinimumSize(new Dimension(310, 30));
        fp.setPreferredSize(fp.getMinimumSize());
        fp.setMaximumSize(fp.getPreferredSize());
        ft.setMinimumSize(new Dimension(310, 30));
        ft.setPreferredSize(ft.getMinimumSize());
        ft.setMaximumSize(ft.getPreferredSize());
        cancel.setContentAreaFilled(false);
        cancel.addMouseListener(new MyMouseListener());

    }

    private void initVarFont(){
        Font labelFont = new Font(Font.SANS_SERIF, Font.BOLD, 14);
        fonts.setFont(labelFont);
        fontsMebu.setFont(labelFont);
        fontsPane.setFont(labelFont);
        fontsText.setFont(labelFont);

        fm.setText(start.toString()+UserProp.menuFont.getName()+"\">"+UserProp
                .menuFont.getName()+end1+UserProp.menuFont.getSize()+"</td>");
        fp.setText(start.toString()+UserProp.paneFont.getName()+"\">"+UserProp
                .paneFont.getName()+end1+UserProp.paneFont.getSize()+"</td>");
        ft.setText(start.toString()+UserProp.textFont.getName()+"\">"+UserProp
                .textFont.getName()+end1+UserProp.textFont.getSize()+"</td>");
    }

    public void actionPerformed(ActionEvent e) {
        this.setVisible(true);
    }

}
